<?php

class banner extends Controller {
	
	function banner()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
	    $this->load->model('filemanagementmodel');
	    $data['banner'] = $this->filemanagementmodel->getByPostion('banner');
	    
		$this->load->view('header');
		$this->load->view('banner-form', $data);
		$this->load->view('footer');
	}
	
	function upload()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    
	    $this->load->model('filemanagementmodel');
	    $s = array();
	    
		// save profile image
		if(isset($_FILES) && @$_FILES['txtFiles']['error'] == '0') {
			$config['upload_path'] = './data-image/file_management/';
			$config['allowed_types'] = 'gif|jpg|png|swf';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = 'banner';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtFiles'))
			{
				$this->msg = "ไม่สามารถ upload ได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'position' => $this->input->post('position'),
	               'file_name' => $data['upload_data']['file_name']   
	            );
				$this->filemanagementmodel->update($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
				$s['banner'] = $this->filemanagementmodel->getByPostion('banner');
			}
		}
		 
		$this->load->view('header');
		$this->load->view('banner-form', $s);
		$this->load->view('footer', $this->msg);
	}
}
?>
